<!DOCTYPE html>
<html lang="zh-CN">
<!-- 某场比赛的历史赔率 -->

<head>
    <meta charset="UTF-8" />
    <title>赔率变化列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            /* 水平居中 */
            align-items: flex-start;
            /* 顶部对齐 */
            min-height: 100vh;
            /* 保证全屏背景 */
            padding-top: 20px;
            /* 顶部留空 */
            background: #f0f2f5;
        }

        table {
            border-collapse: collapse;
            width: 95%;
            max-width: 1000px;
            background: #ffffff;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;
            text-align: center;
        }

        th,
        td {
            border: 1px solid #e0e0e0;
            /* padding: 12px 8px; */
            font-size: 14px;
        }

        th {
            background: #004aad;
            color: #ffffff;
            font-weight: 600;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        caption {
            caption-side: top;
            font-size: 20px;
            font-weight: bold;
            padding: 12px;
            color: #333;
        }
    </style>
</head>

<body>
    <table>
        <caption>[<span id="gameName"></span>]</caption>
        <caption> <span id="namelist1"></span> | <span id="namelist2"></span></caption>
        <thead>
            <tr>
                <th>编号</th>
                <th>时间</th>
                <th>平博</th>
                <th>皇冠</th>
                <th>下单信息</th>
            </tr>
        </thead>
        <tbody id="list">
            <!-- 可继续添加更多行 -->
        </tbody>
    </table>
</body>
<script src="/resources/common.js"></script>
<script>
    const params = new URLSearchParams(window.location.search)
    // 获取某个参数
    const id = params.get('id')
    if (id) {
        const socket = new WebSocket(`ws://${IP}:8081`);
        socket.addEventListener("open", function (event) {
            socket.send(JSON.stringify({
                type: "odds_history",
                data: id

            }))
        });
        socket.addEventListener("message", function (event) {
            let data = JSON.parse(event.data)
            if (data.type == "odds_history") {
                odds_history(data)
            }
        });

        socket.addEventListener("close", function (event) {
        })
    }

    function odds_history(data) {
        let home, away, psHome, psAway, homeEn, awayEn;
        let tr = ``
        for (let i = 0; i < data.data.length; i++) {
            let game = data.data[i]
            let time = formatTimestampMs(game.id)
            home = game.home
            away = game.away
            psHome = game.ps_home
            psAway = game.ps_away
            homeEn = game.homeEn
            awayEn = game.awayEn
            let hgaMarket = game.market.hga
            let psMarket = game.market.ps3838
            let hgaMarketHdp
            let hgStr = `封`
            for (let j = 0; j < hgaMarket.length; j++) {
                let hgaMarketHdp = hgaMarket[j]
                if (hgaMarketHdp.type == "滚球让球") {
                    if (hgaMarketHdp) {
                        hgStr = (hgStr + `${hgaMarketHdp.homeOdds} / ${hgaMarketHdp.handicap} / ${hgaMarketHdp.awayOdds}  </br>`).replace('封', '')
                    }
                }
            }
            let psStr = `封`
            for (let j = 0; j < psMarket.handap.length; j++) {
                let psMarketHdp = psMarket.handap[j]
                if (psMarketHdp) {
                    psStr = (psStr + `${psMarketHdp.a} / ${psMarketHdp.v} / ${psMarketHdp.b} </br>`).replace('封', '')
                }
            }
            let orderData = game.orderData ? game.orderData : ''
            let orderHtml = ``;
            if (orderData) {
                let od = JSON.parse(orderData)
                orderHtml = `<div style="text-align: left; font-size: 12px; color: #555;">
                                <div>下单目标: ${od.target}</div>
                                <div>主队赔率: ${od.home_odds}</div>
                                <div>客队赔率: ${od.away_odds}</div>
                                <div>盘口: ${od.handicap}</div>
                                <div>下单金额: ${od.amount}</div>
                                <div>PAM: ${od.pam}</div>
                            </div>`
            }
            tr = tr + ` <tr>
                            <td>${i + 1}</td>
                            <td>${time}</td>
                            <td>${psStr}</td>
                            <td>${hgStr}</td>
                            <td>${orderHtml ? orderHtml : ''}</td>
                        </tr>`
        }
        document.getElementById("gameName").textContent = `${home} v ${away} `
        document.getElementById("list").insertAdjacentHTML('beforeend', tr)
        document.title = `${home} v ${away}`
        document.getElementById("namelist1").textContent = `平博：${psHome} v ${psAway}`
        document.getElementById("namelist2").textContent = `皇冠：${homeEn} v ${awayEn}`
    }


    function formatTimestampMs(ts) {
        const date = new Date(ts)
        return date.getFullYear() + '-' +
            String(date.getMonth() + 1).padStart(2, '0') + '-' +
            String(date.getDate()).padStart(2, '0') + ' ' +
            String(date.getHours()).padStart(2, '0') + ':' +
            String(date.getMinutes()).padStart(2, '0') + ':' +
            String(date.getSeconds()).padStart(2, '0') + '.' +
            String(date.getMilliseconds()).padStart(3, '0')
    }
</script>

</html>